﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using BookClubIt.DTO;
using System.Text.RegularExpressions;

namespace BookClubIt
{
    public partial class Register : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        //    RegisterUser_CreatedUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

        //    Validate();
         //   if (Page.IsValid)
         //   {
                Member member = new Member
                {
                    Username = RegisterUser.UserName,
                    Email = RegisterUser.Email,
                    CreatedDate = DateTime.Now,
                    FirstName = string.Empty,
                    LastName = string.Empty,
                    Biography = string.Empty,
                    Zipcode = string.Empty,
                    AvatarURL = "default_avatar.png",
                    NotificationId = 1,
                    Id = 0
                };

                MemberProcessorInstance.SaveMember(member);
                EmailProcessorInstance.SendWelcomeEmail(member.Id);

                string continueUrl = RegisterUser.ContinueDestinationPageUrl;
                if (String.IsNullOrEmpty(continueUrl))
                {
                    continueUrl = "~/account/";
                }
                Response.Redirect(continueUrl);
            }
      //  }

        //private void Validate()
        //{
        //    Email.Valid = true;

        //    if (Email.Text != string.Empty)
        //    {
        //        Regex reg = new Regex("\\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}\\b");
        //        EmailRequired.Valid = reg.IsMatch(Email.Text);
        //    }
        //    else
        //    {
        //        EmailRequired.Valid = false;
        //    }
        //}
    }
}
